<template>
  <el-row type="flex" align="middle" style="height: 40px; width: 100%">
    <el-col>
      <span>{{nodeData.name}}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <el-col>{{nodeData.manager}}</el-col>
        <el-col>
          <el-dropdown @command="handleCommand">
            <span>
              操作
              <i class="el-icon-arrow-down"/>
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isShow" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="isShow" command="delete">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { deleteDepartments } from "@/api/departments";
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isShow: {
      required: true
    }
  },
  methods: {
    // 选择操作
    handleCommand(type) {
      if (type === "add") {
        // 添加部门
        this.$emit("addDepts", this.nodeData.id);
      } else if (type === "edit") {
        // 编辑部门
        this.$emit("editDepts", this.nodeData.id);
      } else {
        // 删除部门
        this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.deleteDepartments();
        });
      }
    },
    // 删除部门方法
    async deleteDepartments() {
      try {
        await deleteDepartments(this.nodeData.id);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.$emit("delDepts");
      } catch (err) {}
    }
  }
};
</script>
